

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/hexo_weini_blog/img/favicon.png">
  <link rel="icon" href="/hexo_weini_blog/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="weini wang">
  <meta name="keywords" content="">
  <meta name="description" content="基础golang官方网站到golang官网下载Golang go命令 安装好go以后，在命令行中输入go help，查看go语言支持的命令   bug         上报bug build       编译包和依赖 clean       移除对象文件和缓存文件    doc         显示包或者符号的文档    env         输出 go env 信息    fix">
<meta property="og:type" content="website">
<meta property="og:title" content="golang基础">
<meta property="og:url" content="https://weinirumo.gitee.io/hexo_weini_blog/golang/basic.html">
<meta property="og:site_name" content="weini的播客">
<meta property="og:description" content="基础golang官方网站到golang官网下载Golang go命令 安装好go以后，在命令行中输入go help，查看go语言支持的命令   bug         上报bug build       编译包和依赖 clean       移除对象文件和缓存文件    doc         显示包或者符号的文档    env         输出 go env 信息    fix">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-27T03:36:34.000Z">
<meta property="article:modified_time" content="2021-09-28T08:12:14.985Z">
<meta property="article:author" content="weini wang">
<meta property="article:tag" content="golang">
<meta property="article:tag" content="go语言">
<meta property="article:tag" content="基础">
<meta name="twitter:card" content="summary_large_image">
  
  <title>golang基础 - weini的播客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/hexo_weini_blog/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/monokai.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/hexo_weini_blog/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"weinirumo.gitee.io","root":"/hexo_weini_blog/","version":"1.8.12","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/hexo_weini_blog/local-search.xml"};
  </script>
  <script  src="/hexo_weini_blog/js/utils.js" ></script>
  <script  src="/hexo_weini_blog/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 60vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/hexo_weini_blog/">
      <strong>weini的blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo_weini_blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" href="javascript:;" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-list"></i>
                技术积累
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" href="/hexo_weini_blog/2021/09/27/go%E8%AF%AD%E8%A8%80/">
                    
                    Go语言
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" href="/hexo_weini_blog/2021/10/12/php/">
                    
                    PHP
                  </a>
                
              </div>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo_weini_blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo_weini_blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo_weini_blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo_weini_blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/hexo_weini_blog/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="golang基础">
              
            </span>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      <div class="container nopadding-x-md">
        <div class="py-5" id="board"
          >
          
          <div class="container">
            <div class="row">
              <div class="col-12 col-md-10 m-auto">
                

<article class="page-content">
  <h1 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h1><h3 id="golang官方网站"><a href="#golang官方网站" class="headerlink" title="golang官方网站"></a>golang官方网站</h3><p>到<a target="_blank" rel="noopener" href="https://golang.google.cn/dl/">golang官网</a>下载Golang</p>
<h3 id="go命令"><a href="#go命令" class="headerlink" title="go命令"></a>go命令</h3><blockquote>
<p>安装好go以后，在命令行中输入go help，查看go语言支持的命令</p>
</blockquote>
<ul>
<li>bug         上报bug</li>
<li>build       编译包和依赖</li>
<li>clean       移除对象文件和缓存文件</li>
<li>   doc         显示包或者符号的文档</li>
<li>   env         输出 <code>go env</code> 信息</li>
<li>   fix         更新包以使用新版 API</li>
<li>   fmt         格式化源码</li>
<li>   generate    通过处理源文件生成go文件</li>
<li>   get         添加依赖到当前模块并安装之</li>
<li>   install     编译并安装包和依赖</li>
<li>   list        列出包和模块</li>
<li>   mod         模块管理</li>
<li>   run         编译并运行Go程序</li>
<li>   test        测试包</li>
<li>   tool        运行指定的go工具</li>
<li>   version     输出go语言的版本</li>
<li>   vet         上报包里边可能的错误</li>
</ul>
<p>此外，你还可以通过使用<code>go help &lt;topic&gt;</code>来查看具体命令的信息。</p>
<h3 id="1-变量"><a href="#1-变量" class="headerlink" title="1 变量"></a>1 变量</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-comment">// 声明变量</span><br><span class="hljs-keyword">var</span> name <span class="hljs-keyword">string</span><br><span class="hljs-keyword">var</span> age <span class="hljs-keyword">int</span><br><br><span class="hljs-comment">// 声明过的变量如果不赋值，就是类型零值</span><br><br><span class="hljs-comment">// 声明变量类型并赋值</span><br><span class="hljs-keyword">var</span> name <span class="hljs-keyword">string</span> = <span class="hljs-string">&quot;张三&quot;</span> <br><span class="hljs-keyword">var</span> age <span class="hljs-keyword">int</span> = <span class="hljs-number">18</span> <br><br><span class="hljs-comment">// 变量简写</span><br><span class="hljs-keyword">var</span> name = <span class="hljs-string">&quot;李四&quot;</span><br><span class="hljs-keyword">var</span> age = <span class="hljs-number">18</span><br><br><span class="hljs-comment">// 省略var</span><br>name := <span class="hljs-string">&quot;张三&quot;</span><br>age := <span class="hljs-number">18</span><br></code></pre></td></tr></table></figure>

<h4 id="1-1-变量名规则"><a href="#1-1-变量名规则" class="headerlink" title="1.1 变量名规则"></a>1.1 变量名规则</h4><ul>
<li>变量名由字母、数字、下划线组成，且首字符不能是数字</li>
<li>不能是go内置的25个关键字</li>
<li>变量只能声明1次</li>
</ul>
<h4 id="1-2-全局变量"><a href="#1-2-全局变量" class="headerlink" title="1.2 全局变量"></a>1.2 全局变量</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span><br><br><span class="hljs-comment">// 声明全局变量</span><br><span class="hljs-keyword">var</span> name <span class="hljs-keyword">string</span> = <span class="hljs-string">&quot;张三&quot;</span><br><span class="hljs-comment">// 或者</span><br><span class="hljs-keyword">var</span> name = <span class="hljs-string">&quot;张三&quot;</span><br><span class="hljs-comment">// 不能省略var</span><br><span class="hljs-comment">// name := &quot;张三&quot;</span><br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>&#123;<br>  fmt.Println(name)<br>  name = <span class="hljs-string">&quot;Jack Zhang&quot;</span><br>  fmt.Println(name)<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="1-3-局部变量"><a href="#1-3-局部变量" class="headerlink" title="1.3 局部变量"></a>1.3 局部变量</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span><br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>&#123;<br>  <span class="hljs-comment">// 声明局部变量，在当前函数可用</span><br>  <span class="hljs-keyword">var</span> name <span class="hljs-keyword">string</span> = <span class="hljs-string">&quot;张三&quot;</span><br>  fmt.Println(name)<br><br>  <span class="hljs-keyword">if</span> <span class="hljs-literal">true</span> &#123;<br>    <span class="hljs-comment">// 声明局部变量， 再当前if中可用</span><br>    <span class="hljs-keyword">var</span> age = <span class="hljs-number">18</span><br>    fmt.Println(age)<br>  &#125;<br>  <br>  <span class="hljs-comment">// 下面的语句就会报错，因为age 再if括号的作用域中</span><br>  <span class="hljs-comment">// fmt.Println(age)</span><br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="类型"><a href="#类型" class="headerlink" title="类型"></a>类型</h3><h4 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-comment">// 有个人他的名字叫张三</span><br><span class="hljs-keyword">var</span> name <span class="hljs-keyword">string</span> = <span class="hljs-string">&quot;张三&quot;</span><br><span class="hljs-comment">// 张三的女朋友</span><br><span class="hljs-keyword">var</span> girlFriendName = <span class="hljs-string">&quot;翠花&quot;</span><br></code></pre></td></tr></table></figure>

<h3 id="关键字"><a href="#关键字" class="headerlink" title="关键字"></a>关键字</h3><p><code>go语言</code>有25个关键字不能用于变量名</p>
<table>
<thead>
<tr>
<th>break</th>
<th>default</th>
<th>func</th>
<th>interface</th>
<th>select</th>
</tr>
</thead>
<tbody><tr>
<td>case</td>
<td>defer</td>
<td>go</td>
<td>map</td>
<td>struct</td>
</tr>
<tr>
<td>chan</td>
<td>else</td>
<td>goto</td>
<td>package</td>
<td>switch</td>
</tr>
<tr>
<td>const</td>
<td>fallthrough</td>
<td>if</td>
<td>range</td>
<td>type</td>
</tr>
<tr>
<td>continue</td>
<td>for</td>
<td>import</td>
<td>return</td>
<td>var</td>
</tr>
</tbody></table>


  
</article>

              </div>
            </div>
          </div>
        </div>
      </div>
    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/hexo_weini_blog/js/events.js" ></script>
<script  src="/hexo_weini_blog/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/hexo_weini_blog/js/local-search.js" ></script>



  
    <script  src="/hexo_weini_blog/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/hexo_weini_blog/js/boot.js" ></script>


</body>
</html>
